package com.util;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class GPSUtils {
    private static final String HOSTARR = "http://restapi.amap.com/v3/geocode/regeo";
    private static final String KEY = "d999180714cf9106f478b3715bd3bbad";

    public static String getLocationAddr(String location){
        List<NameValuePair> list = new LinkedList<>();
        list.add(new BasicNameValuePair("key", KEY));
        list.add(new BasicNameValuePair("output", "JSON"));
        list.add(new BasicNameValuePair("location", location));
        String responseEntity = null;
        String addr="";
        try {
            HttpGet httpGet = new HttpGet(new URIBuilder(HOSTARR).setParameters(list).build());
            httpGet.setConfig(RequestConfig.custom().setConnectTimeout(2000).build());
            HttpResponse response = HttpClients.createDefault().execute(httpGet);
            responseEntity = EntityUtils.toString(response.getEntity(), "UTF-8");
            JSONObject jsonObject=JSON.parseObject(responseEntity);
            addr=jsonObject.getJSONObject("regeocode").getJSONObject("addressComponent")
                    .getJSONObject("building").getString("name");
            if(addr.equals("[]")){
                addr=jsonObject.getJSONObject("regeocode").getString("formatted_address");
            }
        } catch (Exception e) {
            if (e instanceof ConnectTimeoutException) {
                System.out.println("-------请求超时-------");
            } else {
                e.printStackTrace();
            }
        }
        return addr;
    }

//public static void main(String[] args) throws InterruptedException {
//        System.out.println(getLocationAddr("113.0217,23.1378"));
//        //广东省佛山市南海区狮山镇S82佛山一环高速辅路
//
//    }


    public static String addressResolution(String address){
        String regex="(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)";
        Matcher m=Pattern.compile(regex).matcher(address);
        String province=null,city=null,county=null,town=null;
        String row=null;
        while(m.find()){
            province=m.group("province");
            city=m.group("city");
            county=m.group("county");
            town=m.group("town");
            System.out.println("tomn"+town);
            row=(province==null?"":province) + (city==null?"":city) + (county==null?"":county) + (town==null?"":town);
            System.out.println("row"+row);
        }
        return row;
    }


}






